﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daycare.Models;
using Daycare.Services;

namespace Daycare.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                login.FindControl("UserName").Focus();
            }
        }

        protected void login_Authenticate(object sender, AuthenticateEventArgs e)
        {
            Literal litFailureText = (Literal)login.FindControl("litFailureText");

            MembershipUser userInfo = Membership.GetUser(login.UserName);
            if (userInfo != null)
            {
                if (userInfo.IsApproved)
                {
                    if (!userInfo.IsLockedOut)
                    {
                        UserService us = new UserService();
                        
                        Password checkPassword = us.GetPasswordInfo(login.UserName);
                        Password typedPassword = PasswordService.EncodePassword(login.Password, checkPassword.salt);

                        if (typedPassword.hashedPassword == checkPassword.hashedPassword)
                        {
                            Employee user = us.GetEmployee(userInfo.UserName);
    
                            if (userInfo.UserName == "admin")
                            {
                                e.Authenticated = true;
                            }
                            else
                            {
                                if (user != null)
                                {
                                    if (user.isEmployed == true)
                                    {
                                        e.Authenticated = true;
                                    }
                                    else
                                    {
                                        litFailureText.Text = "Employee account currently disabled. Please speak to your supervisor.";
                                    }
                                }
                                else
                                {
                                    litFailureText.Text = "Username or password incorrect.";
                                }
                            }
                        }
                        else
                        {
                            litFailureText.Text = "Username or password incorrect.";
                        }
                    }
                    else
                    {
                        litFailureText.Text = "User account is locked out.";
                    }
                }
                else
                {
                    litFailureText.Text = "User account has not yet been approved.";
                }
            }
            else
            {
                litFailureText.Text = "Username or password incorrect.";
            }
        }

        protected void login_LoggedIn(object sender, EventArgs e)
        {
            String returnUrl = Request.QueryString["ReturnUrl"];

            if (returnUrl != null)
            {
                Response.Redirect(Request.QueryString["ReturnUrl"], true);
            }
        }
    }
}